<script lang="ts" setup>
  import { useGuessList } from '@/composables'
  import { useMemberStore } from '@/stores'
  import { getMoneyAPI } from '@/services/wallet'
  import { onUpdated, ref } from 'vue'
  import { onReady, onShow } from '@dcloudio/uni-app'
  // 获取屏幕边界到安全区域距离
  const { safeAreaInsets } = uni.getSystemInfoSync()
  const safeAreaInsetsTop = ref(safeAreaInsets?.top || 0)
  if (safeAreaInsetsTop?.value < 30) {
    safeAreaInsetsTop.value = 40
  }

  // 订单选项
  const orderTypes = [
    { type: '1', text: '个人账户', icon: 'icon-currency', number: 444 },
    { type: '2', text: '企业账户', icon: 'icon-gift', number: 333 },
    { type: '3', text: '剩余积分', icon: 'icon-check', number: 222 },
    { type: '4', text: '优惠券', icon: 'icon-comment', number: 111 },
  ]

  // 退出登录
  const onLogout = () => {
    // 模态弹窗
    uni.showModal({
      content: '是否退出登录？',
      success: (res) => {
        if (res.confirm) {
          // 清理用户信息
          memberStore.clearProfile()
          // 跳到登录页
          moneyval.value = '0'
          uni.navigateTo({ url: '/pages/login/login' })
        }
      },
    })
  }
  // 获取会员信息
  const memberStore = useMemberStore()

  const moneyval = ref('0')
  const { guessRef, onScrolltolower } = useGuessList()

  async function getmoney() {
    if (memberStore.profile) {
      const res = await getMoneyAPI()
      if (res.code == '200') {
        moneyval.value = res.data.money
      }
    }
  }

  onReady(() => {
    getmoney()
  })

  onShow(() => {
    getmoney()
  })

  const gotoMerny = () => {
    if (!memberStore.profile) {
      uni.navigateTo({ url: '/pages/login/login' })
    } else {
      uni.navigateTo({ url: '/pages/wallet/wallet' })
    }

  }
</script>

<template>
  <scroll-view class="viewport" enable-back-to-top scroll-y @scrolltolower="onScrolltolower">
    <!-- 个人资料 -->
    <view :style="{ paddingTop: safeAreaInsetsTop + 'px' }" class="profile">
      <!-- 情况1：已登录 -->
      <view v-if="memberStore.profile" class="overview">
        <view>
          <image :src="memberStore.profile.avatar" class="avatar" mode="aspectFill"></image>
        </view>
        <view class="meta">
          <view class="nickname">
            {{ memberStore.profile.nickname || memberStore.profile.account }}
          </view>
        </view>
      </view>
      <!-- 情况2：未登录 -->
      <view v-else class="overview">
        <navigator hover-class="none" url="/pages/login/login">
          <image class="avatar gray" mode="aspectFill"
            src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png">
          </image>
        </navigator>
        <view class="meta">
          <navigator class="nickname" hover-class="none" url="/pages/login/login">
            未登录
          </navigator>
          <view class="extra">
            <text class="tips">点击登录账号</text>
          </view>
        </view>
      </view>
      <navigator v-if="memberStore.profile?.ismanager == 1" class="settings" hover-class="none"
        url="/pagesMember/settings/settings">
        管理员入口
      </navigator>
    </view>

    <!-- 我的钱包 -->
    <view class="maimeney">
      <navigator @click="gotoMerny">
        <view class="maimeney-title"> 我的钱包</view>
        <view class="maimeney-section">
          <!-- 钱包 -->
          <view class="maimeney-button">
            <text>余额：{{moneyval}}￥</text>
          </view>
          <view>
            <button style="margin-left: 10px;" class="mini-btn" type="primary" size="mini">充值余额</button>
          </view>
        </view>
      </navigator>
    </view>


    <!-- 我的订单 -->
    <view class="orders">
      <view class="title"> 我的订单</view>
      <view class="section">
        <!-- 订单 -->
        <navigator url="/pages/list/list">
          <view class="order-button">
            <image class="image" src="/static/tabs/cart_default.png" />
            <text>订单</text>
          </view>
        </navigator>
      </view>
    </view>

    <!-- 管理员表达-->
    <!-- 列表2 -->
    <!-- 操作按钮 -->
    <view v-if="memberStore.profile" class="action">
      <view class="button" @tap="onLogout">退出登录</view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
  page {
    height: 100%;
    overflow: hidden;
    background-color: #f7f7f8;
  }

  .viewport {
    height: 100%;
    background-repeat: no-repeat;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/center_bg.png);
    background-size: 100% auto;
  }

  /* 用户信息 */
  .profile {
    margin-top: 20rpx;
    position: relative;

    .overview {
      display: flex;
      height: 120rpx;
      padding: 0 36rpx;
      color: #fff;
    }

    .avatar {
      width: 120rpx;
      height: 120rpx;
      border-radius: 50%;
      background-color: #eee;
    }

    .gray {
      filter: grayscale(100%);
    }

    .meta {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: flex-start;
      line-height: 30rpx;
      padding: 16rpx 0;
      margin-left: 20rpx;
    }

    .nickname {
      max-width: 180rpx;
      margin-bottom: 16rpx;
      font-size: 30rpx;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .extra {
      display: flex;
      font-size: 20rpx;
    }

    .tips {
      font-size: 22rpx;
    }

    .update {
      padding: 3rpx 10rpx 1rpx;
      color: rgba(255, 255, 255, 0.8);
      border: 1rpx solid rgba(255, 255, 255, 0.8);
      margin-right: 10rpx;
      border-radius: 30rpx;
    }

    .settings {
      position: absolute;
      bottom: 0;
      right: 40rpx;
      font-size: 30rpx;
      color: #fff;
    }
  }


  .maimeney {
    position: relative;
    z-index: 99;
    padding: 30rpx;
    height: 150rpx;
    overflow: hidden;
    margin: 50rpx 20rpx 0;
    background-image: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
    border-radius: 10rpx;
    box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);

    .maimeney-title {
      height: 40rpx;
      line-height: 40rpx;
      color: #1e1e1e;
    }

    .maimeney-section {
      width: 100%;
      display: flex;
      justify-content: center;

      .maimeney-button {
        font-size: 38rpx;
      }
    }
  }

  /* 我的订单 */
  .orders {
    position: relative;
    z-index: 99;
    padding: 30rpx;
    margin: 50rpx 20rpx 0;
    background-color: #fff;
    border-radius: 10rpx;
    box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);

    .title {
      height: 40rpx;
      line-height: 40rpx;
      font-size: 28rpx;
      color: #1e1e1e;

      .navigator {
        font-size: 24rpx;
        color: #939393;
        float: right;
      }
    }

    .section {
      width: 100%;
      display: flex;
      justify-content: space-around;
      padding: 40rpx 20rpx 10rpx;

      .navigator,
      .contact {
        text-align: center;
        font-size: 24rpx;
        color: #333;

        &::before {
          display: block;
          font-size: 60rpx;
          color: #ff9545;
        }

        &::after {
          border: none;
        }
      }

      .order-button {
        display: flex;
        flex-direction: column;
        align-items: center;

        .image {
          height: 60rpx;
          width: 60rpx;
        }
      }

      .contact {
        padding: 0;
        margin: 0;
        border: 0;
        background-color: transparent;
        line-height: inherit;
      }
    }
  }

  /* 猜你喜欢 */
  .guess {
    background-color: #f7f7f8;
    margin-top: 20rpx;
  }

  /* 列表 */
  .list {
    padding: 0 20rpx;
    background-color: #fff;
    margin-bottom: 20rpx;
    border-radius: 10rpx;

    .item {
      line-height: 90rpx;
      padding-left: 10rpx;
      font-size: 30rpx;
      color: #333;
      border-top: 1rpx solid #ddd;
      position: relative;
      text-align: left;
      border-radius: 0;
      background-color: #fff;

      &::after {
        width: auto;
        height: auto;
        left: auto;
        border: none;
      }

      &:first-child {
        border: none;
      }

      &::after {
        right: 5rpx;
      }
    }

    .arrow::after {
      content: '\e6c2';
      position: absolute;
      top: 50%;
      color: #ccc;
      font-family: 'erabbit' !important;
      font-size: 32rpx;
      transform: translateY(-50%);
    }
  }

  /* 操作按钮 */
  .action {
    text-align: center;
    line-height: 90rpx;
    margin-top: 40rpx;
    font-size: 32rpx;
    color: #333;

    .button {
      background-color: #fff;
      margin-bottom: 20rpx;
      border-radius: 10rpx;
    }
  }
</style>
